package com.ecloud.service.security.service.impl;

import java.time.LocalDateTime;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.common.base.result.Result;
import com.ecloud.common.base.result.ResultBuilder;
import com.ecloud.common.base.result.ResultStatus;
import com.ecloud.common.base.result.RowsBuilder;
import com.ecloud.openapi.security.convert.BaseMenuConvert;
import com.ecloud.openapi.security.dto.BaseMenuDto;
import com.ecloud.openapi.security.po.BaseMenuPo;
import com.ecloud.openapi.security.vo.BaseMenuVo;
import com.ecloud.service.security.dao.BaseMenuDao;
import com.ecloud.service.security.service.iface.IBaseMenuService;

/**
 * 机构菜单表 服务实现类
 *
 * @author LiuGangQiang Create in 2021/07/04
 */
@Service
public class BaseMenuService extends ServiceImpl<BaseMenuDao, BaseMenuPo> implements IBaseMenuService {

    /**
     * @see com.ecloud.service.security.service.iface.IBaseMenuService#insert(com.ecloud.openapi.security.model.dto.BaseMenuDto)
     */
    @Override
    public Result<?> insert(BaseMenuDto dto) {
        /* 检查菜单是否有冲突 */
        LambdaQueryWrapper<BaseMenuPo> conflictWrapper = new LambdaQueryWrapper<>();
        conflictWrapper.eq(BaseMenuPo::getCode, dto.getCode());
        conflictWrapper.eq(BaseMenuPo::getEnabled, true);
        BaseMenuPo conflict = baseMapper.selectOne(conflictWrapper);
        if (conflict != null) {
            /* 存在冲突则返回提示 */
            return ResultBuilder.build(ResultStatus.CONFLICT);
        }
        /* 不冲突设置默认信息并新增数据 */
        BaseMenuPo entity = BaseMenuConvert.INSTANCE.dtoToPo(dto);
        entity.setUpdateTime(LocalDateTime.now());
        entity.setCreateTime(LocalDateTime.now());
        if (baseMapper.insert(entity) > 0) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.service.security.service.iface.IBaseMenuService#delete(com.ecloud.openapi.security.model.dto.BaseMenuDto)
     */
    @Override
    public Result<?> delete(BaseMenuDto dto) {
        /* 检查菜单是否有存在 */
        LambdaQueryWrapper<BaseMenuPo> existsWrapper = new LambdaQueryWrapper<>();
        existsWrapper.eq(BaseMenuPo::getId, dto.getId());
        BaseMenuPo exists = baseMapper.selectOne(existsWrapper);
        if (exists == null) {
            /* 不存在则返回提示 */
            return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND);
        }
        /* 存在则删除 */
        if (baseMapper.deleteById(dto.getId()) > 0) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.service.security.service.iface.IBaseMenuService#update(com.ecloud.openapi.security.model.dto.BaseMenuDto)
     */
    @Override
    public Result<?> update(BaseMenuDto dto) {
        /* 检查菜单是否有存在 */
        LambdaQueryWrapper<BaseMenuPo> existsWrapper = new LambdaQueryWrapper<>();
        existsWrapper.eq(BaseMenuPo::getId, dto.getId());
        BaseMenuPo exists = baseMapper.selectOne(existsWrapper);
        if (exists == null) {
            /* 不存在则返回提示 */
            return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND);
        }
        /* 检查菜单是否有冲突 */
        LambdaQueryWrapper<BaseMenuPo> conflictWrapper = new LambdaQueryWrapper<>();
        conflictWrapper.ne(BaseMenuPo::getId, dto.getId());
        conflictWrapper.eq(BaseMenuPo::getCode, dto.getCode());
        conflictWrapper.eq(BaseMenuPo::getEnabled, true);
        BaseMenuPo conflict = baseMapper.selectOne(conflictWrapper);
        if (conflict != null) {
            /* 存在冲突则返回提示 */
            return ResultBuilder.build(ResultStatus.CONFLICT);
        }
        /* 存在数据并且数据不冲突执行修改 */
        dto.setUpdateTime(LocalDateTime.now());
        if (baseMapper.updateByDto(dto) > 0) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.service.security.service.iface.IBaseMenuService#queryList(com.ecloud.openapi.security.model.dto.BaseMenuDto)
     */
    @Override
    @Cacheable(value = "menu", unless = "#result.code != 200")
    public Result<?> queryList(BaseMenuDto dto) {
        IPage<BaseMenuVo> page = baseMapper.queryList(new Page<>(dto.getPage(), dto.getRows()), dto);
        if (page.getTotal() > 0) {
            return ResultBuilder.build(ResultStatus.OK, RowsBuilder.build(page.getTotal(), page.getRecords()));
        }
        return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND);
    }

    /**
     * @see com.ecloud.service.security.service.iface.IBaseMenuService#queryDetail(java.lang.String)
     */
    @Override
    public Result<?> queryDetail(String id) {
        BaseMenuVo vo = baseMapper.queryOne(id);
        if (vo != null) {
            return ResultBuilder.build(ResultStatus.OK, vo);
        }
        return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND);
    }

}
